package com.pan.action.chain.impl;

import com.pan.domain.Dot;
import com.pan.action.chain.ChainAction;
import com.pan.show.factory.RecordFactory;
import com.pan.show.record.Record;
import com.pan.utils.ApplicationConfig;
import com.pan.utils.MyMouseInfo;
import com.pan.utils.ShortcutKeyConfig;

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.Set;

public class DistanceAction extends ChainAction<Set<String>> {



    private String[] targetKeys;{
        String distanceShortcutKey = ShortcutKeyConfig.INSTANCE.getDistanceKey();
        targetKeys = distanceShortcutKey.trim().split("\\+");
    }

    public DistanceAction() {
    }

    private Dot previousDot;
    @Override
    public void handle(Set<String> keys) {
        for (String key:targetKeys) {
            if (!keys.contains(key)) return;
        }
        if (previousDot == null){
            Point location = MyMouseInfo.INSTANCE.getPoint();//MouseInfo.getPointerInfo().getLocation();
            int x = (int) location.getX();
            int y = (int) location.getY();
            previousDot = new Dot(x,y);
            return;
        }
        //todo:
        Point location = MyMouseInfo.INSTANCE.getPoint();//MouseInfo.getPointerInfo().getLocation();
        int x = location.x;
        int y = location.y;
        Dot currentDot = new Dot(x,y);
        int distanceX = currentDot.getX() - previousDot.getX();
        int distanceY = currentDot.getY() - previousDot.getY();
        distanceX = distanceX<0?-distanceX:distanceX;
        distanceY = distanceY<0?-distanceY:distanceY;
        double distance = Math.sqrt(distanceX*distanceX+distanceY*distanceY);
        String distanceStr = "X Distance: "+distanceX+"\n" +
                            "Y Distance: " +distanceY+"\n"+
                            "Two-Point Distance: "+(distance+"").substring(0,(distance+"").indexOf(".")+3)+"\n";
        String showString = "Previous Point: \n" + previousDot.toString()+"Current Point: \n"+currentDot.toString()+distanceStr;
        RecordFactory recordFactory = ApplicationConfig.INSTANCE.getRecordFactory();
        Record record = recordFactory.getRecord();
        record.setContent(showString);
        record.display();

//        JFrame jFrame = new JFrame("distance");
//        jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//        JLabel label = new JLabel(showString);
//        jFrame.add(label);
//        jFrame.pack();
//        jFrame.setVisible(true);
        previousDot = null;

    }
}

